package com.codeczx.mygeeknews.zhihu;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.codeczx.mygeeknews.DailyListBean;
import com.codeczx.mygeeknews.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class DailyFragment extends Fragment implements DailyContract.View {

    DailyPresenter mPresenter;
    @BindView(R.id.rv_zhihu_daily)
    RecyclerView rvZhihuDaily;
    Unbinder unbinder;
    ZhihuDailyAdapter adapter;

    public DailyFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = new DailyPresenter();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_daily, container, false);
        unbinder = ButterKnife.bind(this, view);
        rvZhihuDaily.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new ZhihuDailyAdapter(null);
        rvZhihuDaily.setAdapter(adapter);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        mPresenter.attachView(this);
        super.onViewCreated(view, savedInstanceState);
        mPresenter.getDailyData();
    }

    @Override
    public void onDestroyView() {
        mPresenter.detachView();
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void showContent(DailyListBean bean) {
        adapter.updateList(bean.getStories());
    }
}
